package com.pxst.fx.controller;


import com.pxst.exception.BusinessException;
import com.pxst.fx.entity.SysFxOrderChildExcelModel;
import com.pxst.fx.entity.SysFxOrderExcelModel;
import com.pxst.fx.req.*;
import com.pxst.fx.resp.SysFxOrderChildResp;
import com.pxst.fx.resp.SysFxOrderEntityResp;
import com.pxst.fx.service.SysFxOrderChildService;
import com.pxst.fx.service.SysFxOrderEntityService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import com.pxst.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api/fxOrderChild")
@Api(tags = "子下发相关接口")
@Validated
public class SysFxOrderChildController {

    @Autowired
    private SysFxOrderChildService sysFxOrderChildService;

    @ApiOperation("分页查询")
    @GetMapping
    public ApiResp<PageResp<SysFxOrderChildResp>> getSysFxOrderList(SysFxOrderChildReq sysFxOrderEntityReq) {
        return sysFxOrderChildService.getSysFxOrderChildList(sysFxOrderEntityReq);
    }

    @ApiOperation("汇出报表")
    @GetMapping("/download")
    public void download(SysFxOrderChildReq sysFxOrderEntityReq, HttpServletResponse response){
        List<SysFxOrderChildExcelModel> excelModels = sysFxOrderChildService.download(sysFxOrderEntityReq);
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            ExcelUtils.exportExcel(excelModels, "子下发汇出报表","子下发报表", SysFxOrderChildExcelModel.class,dateFormat.format(new Date())+"-SubFX", response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("汇出报表错误");
        }
    }

    @ApiOperation("新增子下发")
    @PostMapping("/add")
    public ApiResp add(SysFxOrderChildInfoReq sysFxOrderChildInfoReq) {
        return sysFxOrderChildService.create(sysFxOrderChildInfoReq);
    }

    @ApiOperation("子下发确认按钮")
    @PutMapping("/{id}/check")
    public ApiResp check(@PathVariable Long id) {
        return sysFxOrderChildService.check(id);
    }

    @ApiOperation("子下发更新和批准按钮")
    @PutMapping("/updateStatus")
    public ApiResp updateStatus(@RequestBody SysFxOrderChildStatusReq sysFxOrderChildStatusReq) {
        return sysFxOrderChildService.updateStatus(sysFxOrderChildStatusReq);
    }

    @ApiOperation("子下发刷新按钮")
    @PutMapping("/{id}/runMon")
    public ApiResp runMon(@PathVariable Long id) {
        return sysFxOrderChildService.runMon(id);
    }

    @ApiOperation("子下发重试按钮")
    @PutMapping("retry")
    public ApiResp retry(SysFxOrderChildRetryReq sysFxOrderChildRetryReq) {
        return sysFxOrderChildService.retry(sysFxOrderChildRetryReq);
    }




}
